package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.StrategyMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadStrategyDTO;
import com.honglv.pojo.entity.Strategy;
import com.honglv.pojo.entity.Strategy;

import com.honglv.pojo.vo.GetStrategiesByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StrategyServiceImpl implements StrategyService {
    @Autowired
    private StrategyMapper strategyMapper;
    /**
     * 用户上传攻略
     * @param uploadStrategyDTO
     * @return
     */
    @Override
    public void upload(UploadStrategyDTO uploadStrategyDTO) {
        Long authorId = BaseContext.getCurrentId();
        Strategy strategy=new Strategy();
        BeanUtils.copyProperties(uploadStrategyDTO,strategy);
        strategy.setAuthorId(authorId);
        strategy.setLike(0L);
        strategy.setViews(0L);
        strategyMapper.insert(strategy);
    }
    /**
     * 根据城市分页获取攻略
     * @param pageByCityDTO
     * @return
     */

    @Override
    public PageVO<GetStrategiesByCityVO> getStrategiesByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Strategy> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Strategy> list = strategyMapper.selectList(qw);
        GetStrategiesByCityVO gf=new GetStrategiesByCityVO();
        List<GetStrategiesByCityVO> Strategies=new ArrayList<>();
        for(Strategy strategy:list){
            BeanUtils.copyProperties(strategy,gf);
            Strategies.add(gf);
        }
        if (Strategies == null){
            return null;
        }
        PageInfo<GetStrategiesByCityVO> pageInfo = new PageInfo<>(Strategies);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }
}
